package com.zp.framework.services.test;

import javax.ws.rs.core.MediaType;

import junit.framework.Assert;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.UniformInterfaceException;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.zp.bean.common.ResultCommonVo;
import com.zp.bean.user.ResultLoginVo;
import com.zp.bean.user.ResultRoleVo;
import com.zp.bean.user.ResultUserVo;
import com.zp.util.Constant;
import com.zp.util.MD5Util;

/**
 * test Type services function with Junit
 * 
 * @author chenlijun08@gmail.com
 * 
 * @version 1.0
 * 
 * @date 2014/2/9
 * 
 */

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:configure/applicationContext.xml" })
public class RunUserServices {

	private static final Log logger = LogFactory.getLog(RunUserServices.class);
	private static final String URI = "http://localhost:8082/ZP/rest/";
	private WebResource service;

	@Before
	public void init() {
		ClientConfig config = new DefaultClientConfig();
		Client client = Client.create(config);
		service = client.resource(URI);
		logger.info("*********** testing to initial services client ****************");
	}

	@After
	public void destory() {
		service = null;
		logger.info("*********** testing to destory services instance ******************");
	}

	@Test
	public void testRegisterUser() {
		try {
			JSONObject register = new JSONObject();
			try {
				register.put("username", "小菲");
				register.put("password", "123456");
			} catch (JSONException e) {
				e.printStackTrace();
			}
			ClientResponse response = service.path("user/registerUser")
					.queryParam("jsonContext", register.toString())
					.type(MediaType.APPLICATION_JSON_TYPE)
					.post(ClientResponse.class);
			Assert.assertEquals(Constant.OK,
					response.getEntity(ResultCommonVo.class).getMessage());
		} catch (UniformInterfaceException e) {
			System.out.println(e.getResponse().getStatus());
			if (e.getResponse().getStatus() == 404) {
			} else if (e.getResponse().getStatus() == 401) {
				throw new IllegalStateException(e);
			} else if (e.getResponse().getStatus() == 403) {
				throw new IllegalStateException(e);
			} else {
				throw e;
			}
		}

	}

	/** @Test **/
	public void testfindUserById() {
		String id = "402881e64459ad0e014459adc3b00001";
		ClientResponse response = service.path("user/getUser")
				.queryParam("userId", id).get(ClientResponse.class);
		Assert.assertEquals("Alex", response.getEntity(ResultUserVo.class)
				.getUserInfo().getLogin().getUsername());
	}

	/** @Test **/
	public void testUpdatePassword() {
		JSONObject o = new JSONObject();
		try {
			o.put("userId", "402881e64459ad0e014459adc3b00001");
			o.put("orginalPassword", MD5Util.MD5password("123456"));
			o.put("newPassword", MD5Util.MD5password("2345678"));
			ClientResponse response = service.path("user/updatePass")
					.queryParam("updatePass", o.toString())
					.type(MediaType.APPLICATION_JSON_TYPE)
					.get(ClientResponse.class);
			Assert.assertEquals(Constant.OK,
					response.getEntity(ResultCommonVo.class).getMessage());
		} catch (JSONException e) {
			e.printStackTrace();
		}

	}

	/** @Test **/
	public void testGetUsersByCriteria() {

		ClientResponse response = service.path("user/getUsers")
				.queryParam("pageNo", "1").queryParam("pageSize", "5")
				.type(MediaType.APPLICATION_JSON_TYPE)
				.get(ClientResponse.class);
		ResultUserVo result = response.getEntity(ResultUserVo.class);
		Assert.assertEquals(4, result.getTotalCount());
	}

	/** @Test **/
	public void testGetUserRole() {

		ClientResponse response = service.path("user/getUserRole")
				.queryParam("userId", "402881e64459ad0e014459adc3b0001")
				.type(MediaType.APPLICATION_JSON_TYPE)
				.post(ClientResponse.class);
		ResultRoleVo result = response.getEntity(ResultRoleVo.class);
		Assert.assertEquals(1, result.getTotalCount());
	}
	/**@Test**/
  public void testLogin(){
	  ClientResponse response = service.path("user/login")
				.queryParam("loginName", "HHHHHH").queryParam("loginPass", "DDDDDDDD").queryParam("sysName", "")
				.type(MediaType.APPLICATION_JSON_TYPE)
				.post(ClientResponse.class);
	    ResultLoginVo result = response.getEntity(ResultLoginVo.class);
	    logger.info(result.getLogin().getId());
		Assert.assertEquals(0, result.getTotalCount());
	  
  }
}
